from rest_framework.permissions import DjangoModelPermissions, IsAdminUser
from  rest_framework.viewsets import ModelViewSet
from  apps.goods.models import SKU,SPU
from  apps.wthg_admin.serializers.sku import SKUModelSerializer,SKUPageNumberPagination
from rest_framework.generics import ListAPIView
from apps.goods.models import GoodsCategory
from apps.wthg_admin.serializers.sku import GoodsCategoryModelSerializer
from  apps.wthg_admin.serializers.sku import GoodsSPUModelSerializer
from apps.goods.models import SPUSpecification
from  apps.wthg_admin.serializers.sku import SPUSpecificationModelSerializer
from rest_framework_simplejwt.authentication import JWTAuthentication



class SKUModelViewSet(ModelViewSet):
    """sku增删改查管理"""
    # queryset = SKU.objects.all()
    serializer_class = SKUModelSerializer
    pagination_class = SKUPageNumberPagination
    permission_classes = [DjangoModelPermissions,IsAdminUser]

    # 自定义queryset业务逻辑
    def get_queryset(self):
        if self.request.query_params.get('keyword') is None:
            return SKU.objects.all()

        return SKU.objects.filter(name__contains=self.request.query_params.get('keyword'))


class GoodsCategoryListAPIView(ListAPIView):
    """查询三级视图GoodsCategory"""

    queryset = GoodsCategory.objects.filter(subs=None)
    serializer_class = GoodsCategoryModelSerializer



class GoodsSPUListAPIView(ListAPIView):
    """获取SPU表名称数据"""
    queryset = SPU.objects.all()
    serializer_class = GoodsSPUModelSerializer



class SpecsListAPIView(ListAPIView):
    """获取SPU商品规格信息"""
    def get_queryset(self):
        return SPUSpecification.objects.filter(spu_id=self.kwargs['pk'])
    serializer_class = SPUSpecificationModelSerializer




